package rl.educational.util;



/**
 * Classe di utility per la gestione delle stringhe
 */
public class StringHandler {
	/**
	 * Costante per indicare di riempire a sx (testo allineato a dx)
	 */
	public final static int FILLSX = 0;
	/**
	 * Costante per indicare di riempire a dx (testo allineato a sx)
	 */
	public final static int FILLDX = 1;
	/**
	 * Costante per indicare di riempire in egual misura a sx e dx (testo centrato)
	 */
	public final static int FILLCENTER = 2;

	/**
	 * Restituisce una stringa fillata
	 * 
	 * @param s string da fillare
	 * @param fillPolicy tipo di allineamento
	 * @see #FILLSX
	 * @see #FILLDX
	 * @see #FILLCENTER
	 * @param length lunghezza da raggiungere
	 * @param fillChar carattere di riempimento
	 * @return string fillata
	 */
	public static String fillString(String s, int fillPolicy, int length, char fillChar) {
		String w = s;
		if (w == null) {
			w = "";
		}
		String sfill = "";
		//
		switch (fillPolicy) {
			case FILLDX: {
				if (w.length() > length) {
					w = w.substring(0, length);
				} else if (w.length() < length) {
					StringBuffer sb = new StringBuffer(w);
					int wlen = w.length();
					for (int i = 0; i < length - wlen; i++)
						sb.append(fillChar);
					w = sb.toString();
				}
				break;
			}
			case FILLCENTER: {
				if (w.length() < length) {
					int preString = (length - w.length()) / 2;
					int postString = length - preString - w.length();
					//
					StringBuffer sb = new StringBuffer();
					for (int i = 0; i < preString; i++)
						sb.append(fillChar);
					//
					sb.append(w);
					//
					for (int i = 0; i < postString; i++)
						sb.append(fillChar);
					//
					w = sb.toString();
				} else {
					w = w.substring(0, length);
				}
				break;
			}
			case FILLSX:
			default: {
				if (w.length() > length) {
					w = w.substring(w.length() - length);
				} else if (w.length() < length) {
					for (int i = 0; i < length - w.length(); i++)
						sfill = sfill + fillChar;
					w = sfill + w;
				}
				break;
			}
		}
		return w;
	}

	public static String replaceString(String s, String oldValue, String newValue) {
		StringBuffer strReturn = new StringBuffer("");
		int intIndex = s.indexOf(oldValue);
		if (intIndex != -1) {
			strReturn.append(s.substring(0, ((intIndex > 0) ? intIndex : 0)));
			strReturn.append(newValue);
			strReturn.append(replaceString(s.substring(intIndex + oldValue.length()), oldValue, newValue));
		} else {
			strReturn.append(s);
		}
		return strReturn.toString();
	}

	public static boolean MatchString(String value, String substring) {
		boolean returnItem = false;
		String submatch = replaceString(substring, "*", "");
		if (substring.startsWith("*") && substring.endsWith("*")) {
			returnItem = (value.indexOf(submatch) != -1);
		} else if (substring.startsWith("*")) {
			returnItem = value.endsWith(submatch);
		} else if (substring.endsWith("*")) {
			returnItem = value.startsWith(submatch);
		}
		return returnItem;
	}

}
